//
//  PopViewController.m
//  Karaoke
//
//  Created by 蒋 浩锋 on 12-8-13.
//  Copyright (c) 2012年 Jiang Haofeng. All rights reserved.
//

#import "PopViewController.h"
#import "FoodAddInfoCell.h"
#import "ServerNetworkCenter.h"
#import "SocketDataHandle.h"
#import "ClientNetworkCenter.h"

@interface PopViewController () <UITableViewDataSource, UITableViewDelegate, SocketDataHandleDelegate>
{
    __weak IBOutlet FXLabel *lblTitle;

    __weak IBOutlet UITableView *addInfoTableView;
    NSArray                     *foodInfoArray;
    NSArray                     *tmpFolios;
    NSArray                     *_packChildArray;
    int                         selectedIndex;
    NSString                    *addInfo;
}

@end

@implementation PopViewController
@synthesize delegate;
@synthesize amountLabel;
@synthesize tempFoodObject;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)
    {
        // Custom initialization
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [SocketDataHandle sharedInstance].delegate = self;
    lblTitle.text = tempFoodObject.NameChs;
    [[ServerNetworkCenter sharedInstance] sentMsg:106:self.tempFoodObject.AddInfoNo2, nil];
}

- (void)viewDidUnload
{
    [self setAmountLabel:nil];
    [self setTempFoodObject:nil];
    lblTitle = nil;
    addInfoTableView = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

- (void)socketDataHandleDidAddInfoOfFood:(NSArray *)addInfoDict
{
    foodInfoArray = addInfoDict;
    [addInfoTableView reloadData];
}

- (IBAction)onClickAction:(UIButton *)sender forEvent:(UIEvent *)event
{
    if (sender.tag == 11)
    {
        if (amountLabel.text.length > 0)
        {
            amountLabel.text = [amountLabel.text substringToIndex:amountLabel.text.length - 1];
        }
    }
    else
    if (sender.tag == 12)
    {
        if (amountLabel.text.intValue > 0)
        {
            [SocketDataHandle sharedInstance].Amount = amountLabel.text.integerValue;
            if (!addInfo.length > 0)
            {
                if (foodInfoArray.count > 0)
                {
                    addInfo = [foodInfoArray objectAtIndex:0];
                }
            }
            [SocketDataHandle sharedInstance].addInfo = addInfo;
            [[ServerNetworkCenter sharedInstance] sentMsg:rtSelectFood:@"0", [DataCenter sharedInstance].roomNo, self.tempFoodObject.No, self.amountLabel.text, @"1", [DataCenter sharedInstance].clerkNo, nil];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc]   initWithTitle       :@"提示信息"
                                                        message             :@"请输入数量！"
                                                        delegate            :self
                                                        cancelButtonTitle   :@"确定"
                                                        otherButtonTitles   :nil, nil];
            [alert show];
        }
    }
    else
    {
        if (amountLabel.text.length < 2)
        {
            amountLabel.text = [NSString stringWithFormat:@"%@%d", amountLabel.text, sender.tag];
        }
    }
}

- (void)socketDataHandleDidSelectFood:(NSArray *)foods
{
    tmpFolios = foods;

    if ([self.tempFoodObject.IfPack isEqualToString:@"1"])
    {
        [[ServerNetworkCenter sharedInstance] sentMsg:rtPackChild:self.tempFoodObject.No, @"", nil];
    }
    else
    {
        if ([[SocketDataHandle sharedInstance].tmpSelectFood.HasMultFreeItems isEqualToString:@"1"])
        {
            // 做自动选择多项项目选择
            [[ServerNetworkCenter sharedInstance] sentMsg:rtAutoFreeFood:[SocketDataHandle sharedInstance].tmpSelectFood.MultFreeItemsSetID, [DataCenter sharedInstance].priceId, nil];
        }

        [[SocketDataHandle sharedInstance].tmpSelectFood.foodSet addObjectsFromArray:tmpFolios];
        [[NSNotificationCenter defaultCenter]
            postNotificationName:@"getDataFromCenter"
            object              :self
            userInfo            :[NSDictionary dictionaryWithObjectsAndKeys:@"updateData", @"code", @"", @"detail", nil]];
        [self.delegate popViewControllerDidConfirm:self];
    }
}

- (void)socketDataHandleDidConfirmInputAmount
{
    [[SocketDataHandle sharedInstance].tmpSelectFood.foodSet addObjectsFromArray:tmpFolios];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"getDataFromCenter" object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"updateData", @"code", @"", @"detail", nil]];
}

- (void)socketDataHandleDidPackChild:(NSDictionary *)childs packCount:(NSInteger)packCount packChildArray:(NSArray *)packChildArray
{
    if (packCount == 0)
    {
        [[NSNotificationCenter defaultCenter]
            postNotificationName:@"getDataFromCenter"
            object              :self
            userInfo            :[NSDictionary dictionaryWithObjectsAndKeys:@"updateData", @"code", @"", @"detail", nil]];
        [self.delegate popViewControllerDidConfirm:self];
        return;
    }
    else
    if (childs)
    {
        _packChildArray = packChildArray;
//        [self performSegueWithIdentifier:@"showPackChildView" sender:packDict];
        [self performSegueWithIdentifier:@"showPackChildView" sender:childs];
    }
}
-(void)socketDataHandleDidAutoFreeFood:(NSMutableDictionary *)packDict packCount:(NSInteger)count
{
    if (count == 0)
    {
        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"getDataFromCenter"
         object              :self
         userInfo            :[NSDictionary dictionaryWithObjectsAndKeys:@"updateData", @"code", @"", @"detail", nil]];
//        [self cancelPopover];
        return;
    }
    else
        if (packDict)
        {
            [self performSegueWithIdentifier:@"showPackChildView" sender:packDict];
        }
}

-(void)packChildViewControllerDiddismiss:(PackChildViewController *)packVC
{
    [packVC dismissViewControllerAnimated:YES completion:nil];
    [self.delegate popViewControllerDidConfirm:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showPackChildView"])
    {
        PackChildViewController *packChlidVC = [segue destinationViewController];
        [packChlidVC setValue:tempFoodObject forKey:@"food"];
        [packChlidVC setValue:tmpFolios forKey:@"selectedFoods"];
        [packChlidVC setValue:[SocketDataHandle sharedInstance].tmpSelectFood.GroupID forKey:@"sGroupID"];
        [packChlidVC setValue:_packChildArray forKey:@"packArray"];
        [packChlidVC setValue:sender forKey:@"dict"];
        packChlidVC.delegate = self;
//        [packChlidVC setValue:self forKey:@"inputViewController"];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return foodInfoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodAddInfoCell *cell = (FoodAddInfoCell *)[tableView dequeueReusableCellWithIdentifier:@"FoodAddInfoCell"];

    if (indexPath.row == selectedIndex)
    {
        cell.selectedImage.image = [UIImage imageNamed:@"com_btn_checked.png"];
    }
    else
    {
        cell.selectedImage.image = [UIImage imageNamed:@"com_btn_check.png"];
    }

    cell.lblName.text = [foodInfoArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    addInfo = [foodInfoArray objectAtIndex:indexPath.row];
    selectedIndex = [indexPath row];
    [addInfoTableView reloadData];
}

@end